package com.cn.aam.checaiduo.ui.homepage.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.bean.me.VoucherB;
import com.cn.aam.checaiduo.ui.homepage.pay.ChooseVoucherActivity;
import com.cn.aam.checaiduo.utils.StringUtil;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.cn.aam.checaiduo.utils.CalendarUtil.getDateString;

/**
 * monker on 2017/6/5.
 */

public class ChooseVoucherAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private int type;
    private VoucherB voucherB;
    private float investAmount;

    private int checkPosition = -1;

    public ChooseVoucherAdapter(VoucherB voucherB, int type) {
        this.voucherB = voucherB;
        this.type = type;
        getChooseItem();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.list_voucher_item, parent, false);
        return new CashCouponHolder(view);

    }

    public void setInvestAmount(String investAmount) {
        this.investAmount = 0;
        if (!TextUtils.isEmpty(investAmount)) {
            this.investAmount = Float.parseFloat(investAmount);
        }
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        CashCouponHolder holder = (CashCouponHolder) viewHolder;
        float mini_invest = 0;
        if (position == 0) {
            holder.tvTag1.setVisibility(View.VISIBLE);
        } else {
            holder.tvTag1.setVisibility(View.INVISIBLE);
        }

        if (type == ChooseVoucherActivity.CASH_COUPON) {
            VoucherB.CashCouponBean bean = voucherB.getCash_coupon().get(position);
            bean.setCheck(checkPosition == position);
            holder.tvTypeName.setText("代金券");
            holder.tvDescription.setText(bean.getDescription());
            holder.tvTime.setText(getDateString(bean.getCreate_time()) + "一" + getDateString(bean.getExpired_time()));
            holder.tvRate.setText(StringUtil.decimalRemoval(bean.getAmount()));
            holder.tvTag.setVisibility(View.VISIBLE);
            holder.cbCheck.setChecked(bean.isCheck());

            String miniInvest = TextUtils.isEmpty(bean.getMini_invest()) ? "0" : bean.getMini_invest();
            mini_invest = Float.parseFloat(miniInvest);

            //mini_invest=0表示使用没限制，还有一种投资金额大于限制使用金额才能使用代金券
            if (mini_invest == 0 || mini_invest <= investAmount) {
//                holder.cbCheck.setEnabled(true);
                holder.rlParent.setBackgroundResource(R.drawable.ic_voucher_cash_coupon);

            } else {
                //否则，代金券不可使用
//                holder.cbCheck.setEnabled(false);
                holder.rlParent.setBackgroundResource(R.drawable.ic_voucher_cash_expired);
            }


        } else {
            VoucherB.RateCouponBean bean = voucherB.getRate_coupon().get(position);
            bean.setCheck(checkPosition == position);
            holder.tvTypeName.setText("加息券");
            holder.rlParent.setBackgroundResource(R.drawable.ic_voucher_rate_coupon);
            holder.tvDescription.setText(bean.getDescription());
            holder.tvTime.setText(getDateString(bean.getCreate_time()) + "一" + getDateString(bean.getExpired_time()));
            holder.tvRate.setText(StringUtil.decimalRemoval(bean.getRate()) + "%");
            holder.tvTag.setVisibility(View.INVISIBLE);
            holder.cbCheck.setChecked(bean.isCheck());
        }
        final float finalMini_invest = mini_invest;
        holder.cbCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == ChooseVoucherActivity.CASH_COUPON) {
                    if (finalMini_invest > investAmount) {
                        WMToast.normal("投资金额大于" + finalMini_invest + "元可用");
                        notifyDataSetChanged();
                        return;
                    }

                }
                if (checkPosition == position) {
                    checkPosition = -1;//取消点中
                } else {
                    checkPosition = position;
                }
                notifyDataSetChanged();
            }
        });


        holder.llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }

        });
    }

    @Override
    public int getItemCount() {
        int size = 0;
        if (voucherB == null) return size;

        if (type == ChooseVoucherActivity.CASH_COUPON) {
            size = voucherB.getCash_coupon().size();
        } else {
            size = voucherB.getRate_coupon().size();
        }

        return size;
    }


    public class CashCouponHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.voucher_ll_parent)
        LinearLayout llParent;
        public View itemView;
        @BindView(R.id.voucher_tv_typeName)
        TextView tvTypeName;
        @BindView(R.id.voucher_tv_rate)
        TextView tvRate;
        @BindView(R.id.voucher_tv_time)
        TextView tvTime;
        @BindView(R.id.voucher_rl_parent)
        RelativeLayout rlParent;
        @BindView(R.id.voucher_tv_description)
        TextView tvDescription;
        @BindView(R.id.voucher_tv_tag2)
        TextView tvTag;
        @BindView(R.id.voucher_ll_check)
        LinearLayout llCheck;
        @BindView(R.id.voucher_tv_tag1)
        TextView tvTag1;
        @BindView(R.id.voucher_cb_check)
        CheckBox cbCheck;

        public CashCouponHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ButterKnife.bind(this, itemView);
            tvTag.setVisibility(View.GONE);
            llCheck.setVisibility(View.VISIBLE);
        }
    }

    public void setChoose(int position) {
        checkPosition = position;
        notifyDataSetChanged();
    }

    public int getChooseItem() {
        return checkPosition;
    }

}
